package jianzhi

func LastRemaining(n int, m int) int {

	nums := make([]bool, n)

	left := n
	index := 0

	for left > 1 {
		step := 0
		for step < m {

			if nums[index] != true {
				step++
				if step == m {
					break
				}
			}

			index++

			if index == n {
				index = 0
			}
		}

		nums[index] = true
		left--
		for nums[index] == true {
			index++
			if index == n {
				index = 0
			}
		}

	}

	for index = 0; index < n; index++ {
		if nums[index] == false {
			break
		}
	}

	return index
}
